/*=============================================================================
#    The element-comm is a tentative program used to test our ideas.           
#                       Copyright (C)  bgao                                    
#
#     This program is free software; you can redistribute it and/or            
#      modify it under the terms of the GNU General Public License             
#     as published by the Free Software Foundation; either version 2           
#         of the License, or (at your option) any later version.               
#
#     This program is distributed in the hope that it will be useful,          
#    but WITHOUT ANY WARRANTY; without even the implied warranty of           
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the            
#             GNU General Public License for more details.                    
#
#    You should have received a copy of the GNU General Public License        
#     along with this program; if not, write to the Free Software             
#      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,             
#                       MA  02110-1301, USA.                                  
#==============================================================================
# $Id$ 
#
# $Author$ 
#
# $Revision$ 
#
# $Date$ 
#
# Description: 
#
=============================================================================*/
#include	"wsn-taskid.h"
#include    "rtos-wsn.h"
#include	"led.h"

static struct wsn_timer wsn_app_timer;
#ifdef _Debug_thread_
extern struct wsn_event edfi_test_event;
#endif
extern void adc_light_task(void);
extern uint8 wsn_app_flag;
extern unsigned long wsn_systime;


void wsn_app_timer_func(struct wsn_timer * timer)
{
	int8 ret;
    ret = WSN_StartTimer(&wsn_app_timer, CONSTAINT_MINI_TO_TICKS(1000));
	ASSERT(ret == 0 );		
	if(wsn_app_flag == 1){
    	WSN_NewTask(APP_TASKID, 0, 0);
	}
}	

void backtask2_entry(void)
{
	/**backtask2 post by task**/
	WSN_debug(0x02cd,0);

}

int Wsn_AppTask(int event)
{
	static int app_count;
	led_toggle(LED_GREEN);
	/** WSN App Task Out: %d ,systime:%ld **/
	WSN_debug(0x8044,2,app_count++,wsn_systime);
#ifdef _AVR_
	wsn_backtask_post(adc_light_task);
#else
	wsn_backtask_post(backtask2_entry);
#endif
  	return 1;

}

void wsn_app_start(void)
{
    /* make a timer for 1 second */
    WSN_InitTimer(&wsn_app_timer, wsn_app_timer_func);
    WSN_StartTimer(&wsn_app_timer, CONSTAINT_MINI_TO_TICKS(450));//450ms

}

int Wsn_EdfiTask_S1(int event)
{
	//static int edfi_count1 = 1;
	//WSN_DebugOut("This is edfi test task section 1: %d times\r\n", edfi_count1++);
#ifdef _Debug_thread_
	WSN_SetEvent(&edfi_test_event);
#endif
	return 0;
}



int Wsn_EdfiTask_S2(int event)
{
	//static int edfi_count2 = 1;
	//WSN_DebugOut("This is edfi test task section 2: %d times\r\n", edfi_count2++);
#ifdef _Debug_thread_
	WSN_ClearEvent(&edfi_test_event);
#endif
	return 1;
}

